package com.example.tellwin.question.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.question.bean.VoiceBean;
import com.example.tellwin.utils.MediaUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/11
 * @Describe ：
 **/
public class VoiceAdapter extends RecyclerView.Adapter<VoiceAdapter.VoiceViewHolder> {

    List<VoiceBean> voiceBeans = new ArrayList<>();
    Context context;
    private OnVoiceItemClick onVoiceItemClick;
    boolean isShowDelete;

    public void setOnVoiceItemClick(OnVoiceItemClick onVoiceItemClick) {
        this.onVoiceItemClick = onVoiceItemClick;
    }

    public void setVoiceBeans(List<VoiceBean> voiceBeans, boolean isShowDelete) {
        this.voiceBeans = voiceBeans;
        this.isShowDelete = isShowDelete;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public VoiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new VoiceViewHolder(LayoutInflater.from(context).inflate(R.layout.item_voice, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VoiceViewHolder holder, int position) {
        VoiceBean bean = voiceBeans.get(position);
        holder.voiceTimeTv.setText(context.getResources().getString(R.string.voice_time, (bean.getTime() / 1000 + 1) + ""));
        holder.deleteIv.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);
        holder.itemVoiceView.setOnClickListener(v -> {
            if (onVoiceItemClick != null) {
                onVoiceItemClick.onVoiceItemClick(position, bean);
            }
        });
        holder.deleteIv.setOnClickListener(v -> {
            if (onVoiceItemClick != null) {
                onVoiceItemClick.onItemDeleteClick(position, bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return voiceBeans.size();
    }

    class VoiceViewHolder extends RecyclerView.ViewHolder {

        LinearLayout itemVoiceView;
        TextView voiceTimeTv;
        ImageView deleteIv;

        public VoiceViewHolder(@NonNull View itemView) {
            super(itemView);
            itemVoiceView = itemView.findViewById(R.id.item_voice_view);
            voiceTimeTv = itemView.findViewById(R.id.voice_time_tv);
            deleteIv = itemView.findViewById(R.id.delete_iv);
        }
    }

    public interface OnVoiceItemClick {
        void onVoiceItemClick(int postion, VoiceBean voiceBean);

        void onItemDeleteClick(int postion, VoiceBean voiceBean);
    }
}
